package yaner.hotel.manager.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.dto.roomType.RoomTypeAddDto;
import yaner.hotel.manager.common.dto.roomType.RoomTypeDto;
import yaner.hotel.manager.common.dto.roomType.RoomTypeUpdateDto;
import yaner.hotel.manager.common.enums.RoomTypeEnums;
import yaner.hotel.manager.common.vo.SelectOptionVo;
import yaner.hotel.manager.common.vo.RoomTypeVo;
import yaner.hotel.manager.resource.dao.IRoomTypeDao;
import yaner.hotel.manager.resource.entity.RoomType;
import yaner.hotel.manager.resource.mapper.IRoomTypeMapper;
import yaner.hotel.manager.resource.service.IRoomTypeService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RoomTypeServiceImpl implements IRoomTypeService {
    private final IRoomTypeDao roomTypeDao;
    private final IRoomTypeMapper roomTypeMapper;

    public RoomTypeServiceImpl(IRoomTypeDao roomTypeDao, IRoomTypeMapper roomTypeMapper) {
        this.roomTypeDao = roomTypeDao;
        this.roomTypeMapper = roomTypeMapper;
    }

    @Override
    public List<RoomTypeVo> roomTypeList(RoomTypeDto roomTypeDto) {
        List<RoomTypeVo> roomTypeVoList = new ArrayList<>();
        List<RoomType> roomTypeList = roomTypeDao.selectList(new QueryWrapper<RoomType>().lambda().orderByDesc(RoomType::getCreateTime));
        roomTypeList.forEach(roomType -> {
            RoomTypeVo roomTypeVo = roomTypeMapper.toVo(roomType);
            roomTypeVoList.add(roomTypeVo);
        });
        return roomTypeVoList;
    }

    @Override
    public RoomTypeVo getRoomTypeInfo(String roomTypeId) {
        RoomType roomType = roomTypeDao.selectById(roomTypeId);
        return roomTypeMapper.toVo(roomType);
    }

    @Override
    public String addRoomTypeInfo(RoomTypeAddDto roomTypeAddDto) {
        List<RoomTypeEnums> roomTypeEnumsList = Arrays.stream(RoomTypeEnums.values())
                .filter(roomType -> roomTypeAddDto.getRoomTypeCode().name().equals(roomType.name()))
                .collect(Collectors.toList());
        roomTypeAddDto.setRoomTypeName(roomTypeEnumsList.get(0).getDesc());
        RoomType roomType = roomTypeMapper.toAddEntity(roomTypeAddDto);
        int insert = roomTypeDao.insert(roomType);
        return String.valueOf(insert);
    }

    @Override
    public String updateRoomTypeInfo(RoomTypeUpdateDto roomTypeUpdateDto) {
        List<RoomTypeEnums> roomTypeEnumsList = Arrays.stream(RoomTypeEnums.values())
                .filter(roomType -> roomTypeUpdateDto.getRoomTypeCode().name().equals(roomType.name()))
                .collect(Collectors.toList());
        roomTypeUpdateDto.setRoomTypeName(roomTypeEnumsList.get(0).getDesc());
        RoomType roomType = roomTypeMapper.toUpdateEntity(roomTypeUpdateDto);
        roomType.setRoomTypeId(roomTypeUpdateDto.getRoomTypeId());
        int i = roomTypeDao.updateById(roomType);
        return String.valueOf(i);
    }

    @Override
    public String deleteRoomTypeInfo(String roomTypeId) {
        int i = roomTypeDao.deleteById(roomTypeId);
        return String.valueOf(i);
    }

    @Override
    public List<SelectOptionVo> roomTypeCodeList(boolean repeatFlag) {
        List<SelectOptionVo> roomTypeCodeVoList = new ArrayList<>();
        List<RoomTypeVo> roomTypeVoList = roomTypeList(null);
        List<RoomTypeEnums> roomTypeEnumsList = roomTypeVoList.stream()
                .map(RoomTypeVo::getRoomTypeCode)
                .collect(Collectors.toList());

        for (int i = 0; i < RoomTypeEnums.values().length; i++) {
            RoomTypeEnums roomTypeEnums = RoomTypeEnums.values()[i];
            SelectOptionVo roomTypeCodeVo = new SelectOptionVo();
            RoomTypeVo typeVo = roomTypeVoList.stream()
                    .filter(roomTypeVo -> roomTypeVo.getRoomTypeCode().getCode().equals(roomTypeEnums.getCode()))
                    .collect(Collectors.toList()).get(0);
            roomTypeCodeVo.setId(Objects.nonNull(typeVo)?typeVo.getRoomTypeId():null);
            roomTypeCodeVo.setValue(roomTypeEnums.getCode());
            roomTypeCodeVo.setLabel(roomTypeEnums.getDesc());
            if (roomTypeEnumsList.contains(roomTypeEnums) && !repeatFlag) {
                roomTypeCodeVo.setDisabled(true);
            }
            roomTypeCodeVoList.add(roomTypeCodeVo);
        }
        return roomTypeCodeVoList;
    }
}
